package com.shouxin.attendance.utils;

import android.support.annotation.Nullable;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

public final class ThreadPool {

    private static ExecutorService executeService;

    private ThreadPool() {
    }

    public static void execute(Runnable runnable) {
        if (executeService == null) {
            executeService = Executors.newFixedThreadPool(10, new ThreadFactory() {
                @Override
                public Thread newThread(@Nullable Runnable r) {
                    Thread thread = new Thread(r);
                    thread.setName("Attendance-ThreadPool");
                    return thread;
                }
            });
        }
        executeService.execute(runnable);
    }

    public static void shutdownNow() {
        if (executeService != null) {
            executeService.shutdownNow();
            executeService = null;
        }
    }
}
